<?php namespace App\Http\Controllers\Admin;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;
use Qiniu\Config;

class AdminController extends Controller {

    public function getIndex()
    {
        $allMenu = Config('site.admin');
        $user = Auth::user();
        if($user->hasRole(['service'])){
            unset($allMenu['admin-system'],
                $allMenu['admin-bonus'],
                $allMenu['admin-financial'],
                $allMenu['admin-rbac']['items'][0],
                $allMenu['admin-rbac']['items'][1]);
        }elseif($user->hasRole(['salesman'])){
            unset($allMenu['admin-system'],
                $allMenu['admin-bonus'],
                $allMenu['admin-financial'],
                $allMenu['admin-rbac']['items'][0],
                $allMenu['admin-rbac']['items'][1]);
        }
        $menu = [];
        foreach($allMenu as $k => $v) {
            $sublink = [];
            foreach ($v['items'] as $node) {
                $sublink[] = $node;
            }

            if (count($sublink)) {
                $menu[$k]['name']   = $v['name'];
                $menu[$k]['id']     = $k;
                $menu[$k]['parent'] = 'root';

                $items = [];
                foreach($sublink as $s) {
                    $items[$s['node']] = [
                        'id'        => $s['node'],
                        'name'      => $s['name'],
                        'parent'    => $k,
                        'url'       => url(str_replace('-', '/', $s['node'])),
                    ];
                }

                $menu[$k]['items'] = $items;
            }
        }


        return view('admin.index', ['menu' => json_encode($menu, true)]);
    }

}
